#include"TcpServer.hpp"
#include"Http.hpp"

void Usage(std::string proc)
{
    std::cout<<"Usage :\n\t"<<proc<<"local_port"
        <<std::endl;
}
std::shared_ptr<HttpResponse>Login(std::shared_ptr<HttpRequest>req)//响应请求
{
    LOG(DEBUG,"--------------------------------\n");
    std::string userdata;
    if(req->Method()=="GET")
    {
        userdata = req->Args();
    }
    else if(req->Method()=="POST")
    {
        userdata = req->Text();
    }
    else
    {
            
    }
    LOG(DEBUG,"enter addr handler ,data is:%s\n",userdata.c_str());
    auto response = Factory::BuildHttpResponose();
    response->AddStatusline(200,"OK");
    response->AddHeader("Conten-type","text/html");
    response->AddText("<html><h1>handler data done</h1></html>");
    LOG(DEBUG,"--------------------------------------\n");
    return response;
}
int main(int argc,char*argv[])
{
    if(argc!=2)
    {
        Usage(argv[0]);
        return 1;
    }
    uint16_t port = std::stoi(argv[1]);
    HttpServer httpservice;
    //按路径进行输入
    httpservice.AddHandle("/login",Login);
    TcpServer tcpsvr(port,std::bind(&HttpServer::handlerHttpReqeust,&httpservice,std::placeholders::_1));
    tcpsvr.Loop();
    return 0;
}